package com.ss.project.xia17user.service;

import com.ss.project.xia17user.dao.entity.Client;
import com.ss.project.xia17user.dao.mapper.ClientMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Oauth2客户端Service
 * @author xia17
 * @since 2020/9/15 22:05
 */
@Service
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor
public class ClientService  {

    private final ClientMapper clientMapper;

    /**
     * 获取客户端信息
     * @param clientId 客户端Id
     * @return Client
     */
    public Client findByClientId(String clientId){
        return clientMapper.selectById(clientId);
    }

    /**
     * 用户是否可以访问该Client
     * @param userId 用户
     * @param clientId  客户端Id
     * @return /
     */
    public boolean checkUserClientPermission(Long userId , String clientId){
        Client client = this.findByClientId(clientId);
        if (client == null){
            return false;
        }
        if (client.getIsPublic()){
            return true;
        }
        return clientMapper.countUserClientByClientIdAndUserId(client.getClientId(),userId) == 1;
    }

}
